package com.example.handheld4_gsignal_app.services;

import android.content.Context;
import com.example.handheld4_gsignal_app.data.Task;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Locale;

/**
 * 任务服务类 - 处理任务管理相关功能
 */
public class TaskService {
    
    private Context context;
    private List<Task> tasks;

    public TaskService(Context context) {
        this.context = context;
        this.tasks = new ArrayList<>();
        initSampleData();
    }

    /**
     * 初始化示例数据
     */
    private void initSampleData() {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.getDefault());
        
        // 添加一些示例任务
        String[] taskTitles = {
            "信号强度测试",
            "GPS定位校准",
            "网络连接检查",
            "数据采集任务",
            "系统性能监控"
        };
        
        String[] taskDescriptions = {
            "在指定区域进行4G信号强度测试",
            "校准GPS定位精度",
            "检查网络连接稳定性",
            "执行数据采集和上报",
            "监控系统运行状态"
        };
        
        String[] statuses = {"待处理", "进行中", "已完成"};
        String[] priorities = {"高", "中", "低"};
        
        for (int i = 0; i < taskTitles.length; i++) {
            Task task = new Task();
            task.setId(i + 1);
            task.setTitle(taskTitles[i]);
            task.setDescription(taskDescriptions[i]);
            task.setStatus(statuses[i % statuses.length]);
            task.setPriority(priorities[i % priorities.length]);
            task.setCreatedAt(sdf.format(new Date(System.currentTimeMillis() - i * 86400000L)));
            task.setUpdatedAt(sdf.format(new Date()));
            task.setAssignedTo("用户" + (i + 1));
            
            tasks.add(task);
        }
    }

    /**
     * 获取所有任务
     */
    public List<Task> getAllTasks() {
        return new ArrayList<>(tasks);
    }

    /**
     * 根据状态获取任务
     */
    public List<Task> getTasksByStatus() {
        return new ArrayList<>(tasks); // 简化实现，实际应该按状态分组
    }

    /**
     * 根据状态筛选任务
     */
    public List<Task> getTasksByStatus(String status) {
        List<Task> filteredTasks = new ArrayList<>();
        for (Task task : tasks) {
            if (status.equals(task.getStatus())) {
                filteredTasks.add(task);
            }
        }
        return filteredTasks;
    }

    /**
     * 根据优先级筛选任务
     */
    public List<Task> getTasksByPriority(String priority) {
        List<Task> filteredTasks = new ArrayList<>();
        for (Task task : tasks) {
            if (priority.equals(task.getPriority())) {
                filteredTasks.add(task);
            }
        }
        return filteredTasks;
    }

    /**
     * 添加新任务
     */
    public void addTask(Task task) {
        task.setId(tasks.size() + 1);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.getDefault());
        task.setCreatedAt(sdf.format(new Date()));
        task.setUpdatedAt(sdf.format(new Date()));
        tasks.add(task);
    }

    /**
     * 更新任务
     */
    public boolean updateTask(Task task) {
        for (int i = 0; i < tasks.size(); i++) {
            if (tasks.get(i).getId() == task.getId()) {
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.getDefault());
                task.setUpdatedAt(sdf.format(new Date()));
                tasks.set(i, task);
                return true;
            }
        }
        return false;
    }

    /**
     * 删除任务
     */
    public boolean deleteTask(int taskId) {
        for (int i = 0; i < tasks.size(); i++) {
            if (tasks.get(i).getId() == taskId) {
                tasks.remove(i);
                return true;
            }
        }
        return false;
    }

    /**
     * 根据ID获取任务
     */
    public Task getTaskById(int taskId) {
        for (Task task : tasks) {
            if (task.getId() == taskId) {
                return task;
            }
        }
        return null;
    }

    /**
     * 获取任务统计信息
     */
    public String getTaskStatistics() {
        int total = tasks.size();
        int completed = 0;
        int inProgress = 0;
        int pending = 0;
        
        for (Task task : tasks) {
            switch (task.getStatus()) {
                case "已完成":
                    completed++;
                    break;
                case "进行中":
                    inProgress++;
                    break;
                case "待处理":
                    pending++;
                    break;
            }
        }
        
        return String.format("总任务: %d, 已完成: %d, 进行中: %d, 待处理: %d", 
            total, completed, inProgress, pending);
    }
}